/*
  学习目标：把操作token的方法封装起来, 复用
*/

interface IAuth {
  token: string;
  refresh_token: string;
}
const TOKEN_KEY = "geek-park-81";
// 存token
export const saveAuth = (auth: IAuth) => {
  localStorage.setItem(TOKEN_KEY, JSON.stringify(auth));
};

// 取token
export const getAuth = (): IAuth => {
  return JSON.parse(localStorage.getItem(TOKEN_KEY) || "{}");
};

// 删除token
export const removeAuth = () => {
  localStorage.removeItem(TOKEN_KEY);
};

// 判断是否存在token
export const hasToken = (): boolean => {
  // 💥!! 强制转换为boolean类型, 类似Boolean()
  return !!getAuth().token;
};
